<?php
/*
 *  权限
 */

namespace app\adminbackstage\controller;
use app\adminbackstage\service\JurisdictionService;
use app\BaseController;
use app\Response;
use think\facade\Session;
use think\facade\Request;
use think\Validate;

//use think\facade\Validate;

/**
 * Description of Index
 *
 * @author asus80
 */
class Jurisdiction extends BaseController {
    //权限
    public function jurisdiction(){
        $uid = Request::param('a_uid');

        $data = JurisdictionService::jurosdictionService($uid);
        if($data == false){
            return Response::wrapError(-200,"无菜单权限");
        }else{
            return Response::wrapData($data);
        }
    }

    //菜单列表
    public function menulist(){
        $post_info = $this->request->post();

        list($data,$count) = JurisdictionService::menuListService($post_info);

        if(!empty($data)){
            return Response::wrapData($data,$count);
        }else{
            return Response::wrapError(0,"无菜单");
        }
    }

    //菜单添加
    public function menuAdd(){
        $a_uid = Request::param('a_uid');
        $superior_id = Request::param('superior_id');
        $menu_name = Request::param('menu_name');
        $link = Request::param('link');
        $Icon = Request::param('Icon');
        $component = Request::param('component');
        $exhibition = Request::param('exhibition');
        $enable = Request::param('enable');
        $sort = Request::param('sort');

        $data = [
            'menu_name'=>$menu_name,
            'superior_id'=>$superior_id,
            'link'=>$link,
            'Icon'=>$Icon,
            'component'=>$component,
            'exhibition'=>$exhibition,
            'enable'=>$enable,
            'sort'=>$sort,
        ];
        try {
            $this->validate($data,'MenuAdd');
        }catch (\Exception $e){
            return Response::wrapError(0,$e->getError());
        }
        $res = JurisdictionService::menuAddService($data);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"添加失败!");
        }
    }

    //菜单修改
    public function menuEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $superior_id = Request::param('superior_id');
        $menu_name = Request::param('menu_name');
        $component = Request::param('component');
        $link = Request::param('link');
        $Icon = Request::param('Icon');
        $exhibition = Request::param('exhibition');
        $enable = Request::param('enable');
        $sort = Request::param('sort');
        $data = [
            'menu_name'=>$menu_name,
            'superior_id'=>$superior_id,
            'link'=>$link,
            'Icon'=>$Icon,
            'component'=>$component,
            'exhibition'=>$exhibition,
            'enable'=>$enable,
            'sort'=>$sort,
        ];
        $res = JurisdictionService::menuEditService($id,$data);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"修改失败!");
        }
    }


    //添加角色
    public function roleAdd(){
        $a_uid = Request::param('a_uid');
        $name = Request::param('name');
        $enable = Request::param('enable');
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $res = JurisdictionService::roleAddService($name,$enable);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"操作失败");
        }

    }


    //角色修改
    public function roleEdit(){
        $a_uid = Request::param('a_uid');
        $data = Request::param();
        $res = JurisdictionService::roleEditService($data);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"操作失败");
        }
    }

    //角色列表
    public function roleList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        list($data,$count) = JurisdictionService::roleListService($page,$num);
        return Response::wrapData($data,$count);
    }

    //角色权限查询
    public function rmList(){
        $a_uid = Request::param('a_uid');
        $rid = Request::param('rid');
        $return = JurisdictionService::rmListService($a_uid,$rid);
        if(!empty($return)){
            return Response::wrapData($return);
        }
        return Response::wrapError(0,"无数据!");
    }
    
    
    //角色权限修改
    public function rmEdit(){
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $rid = Request::param('rid');
        $jur = Request::param('jur');
        $jur = explode(",", $jur);
        JurisdictionService::rmEditService($rid,$jur);
        return Response::wrapData();
    }

    //角色用户查询
    public function ruList(){
        $a_uid = Request::param('a_uid');
        $uid = Request::param('uid');
        $return = JurisdictionService::ruListService($a_uid,$uid);
        if(!empty($return)){
            $return = $return->toArray();
            return Response::wrapData($return);
        }
        return Response::wrapError(0,'无数据');

    }

    //角色用户修改
    public function ruEdit(){
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $rid = Request::param('rid');
        $uid = Request::param('uid');

        $res = JurisdictionService::ruEditService($rid,$uid);

        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"操作失败!");
        }
    }
    
}
